import { defineStore } from "pinia";

export const useUserStore = defineStore("USER", {
  state() {
    return {
      islogin: false,
      member: {},
      token: "",
      token_expired: 0,
      h5Weixin: {},
      systemInfo: {}, // --设备信息
      __key_member: "usemall_member",
      __key_token: "uni_id_token",
      __key_token_expired: "uni_id_token_expired",
      __key_h5Weixin: "__h5Weixn",
    };
  },
  // 和vuex一样
  getters: {},
  // 和vuex一样
  actions: {
    // 登录成功
    login(res) {
      console.log(this);
      console.log(res);
      console.log(this.islogin);
      // 用户已登录
      this.islogin = true;

      this.member = res.member;
      this.token = res.user.token;
      this.token_expired = res.user.tokenExpired;

      // 存储会员 member 数据
      localStorage.setItem({
        key: this.__key_member,
        data: this.member,
      });

      // 存储uid token 数据
      localStorage.setItem({
        key: this.__key_token,
        data: this.token,
      });
      // 存储uid token_expired 数据
      localStorage.setItem({
        key: this.__key_token_expired,
        data: this.token_expired,
      });
    },

    // 加载 Token
    loadToken(state) {
      state.member = localStorage.getItem(state.__key_member);
      state.token = localStorage.getItem(state.__key_token);
      state.token_expired = localStorage.getItem(state.__key_token_expired);

      if (state.token_expired > new Date().getTime()) {
        state.islogin = true;
      }
    },

    // 微信公众号 OAuth
    h5WeixinOAuth(state, res) {
      if (res.openid) {
        localStorage.setItem({
          key: state.__key_h5Weixin,
          data: {
            code: res.code,
            openid: res.openid,
          },
        });

        state.h5Weixin = res;
      }
    },

    // 微信公众号 Token
    h5WeixinToken(state, code) {
      let h5Weixin = localStorage.getItem(state.__key_h5Weixin);
      if (h5Weixin && h5Weixin.code === code) {
        state.h5Weixin = h5Weixin;
      } else {
        state.h5Weixin = {};
        localStorage.removeItem(state.__key_h5Weixin);
      }

      return state.h5Weixin;
    },

    // 注销
    logout(state) {
      state.islogin = false;
      state.member = {};
      state.token = "";
      state.token_expired = 0;

      localStorage.removeItem({
        key: state.__key_member,
      });
      localStorage.removeItem({
        key: "__wxuser",
      });
      localStorage.removeItem({
        key: state.__key_token,
      });
      localStorage.removeItem({
        key: state.__key_token_expired,
      });
    },

    // token 令牌
    token(state, token, token_expired) {
      state.token = token;
      state.token_expired = token_expired;

      // 存储uid token 数据
      localStorage.setItem({
        key: state.__key_token,
        data: state.token,
      });
      // 存储uid token_expired 数据
      localStorage.setItem({
        key: state.__key_token_expired,
        data: state.token_expired,
      });
    },
    checkAuthority(state) {
      console.log(state, this.member);
      if (!this.member.member_id) {
        return false;
      }
      return true;
    },
    // 修改 member整体 数据
    putMember(state, user) {
      console.log(state);
      console.log(user);
      state.member = user;

      // 存储会员 member 数据
      localStorage.setItem({
        key: state.__key_member,
        data: state.member,
      });
    },
    upSystemInfo(state, sysInfo) {
      console.log("更新设备信息");
      console.log(sysInfo);
      state.systemInfo = sysInfo;
    },
  },
});
